package org.xtb.fun4;

import java.util.ArrayList;
import java.util.Comparator;

public class Alphabetizer extends Filter {
    private ArrayList<String> kwicList = new ArrayList<>();
    public Alphabetizer(Pipe input, Pipe output) {
        super(input, output);
    }

    @Override
    protected void transform() {
            while (input.hashNextLine()) {
                String line = input.readerLine();
                kwicList.add(line);
            }
            this.kwicList.sort(new AlphabetizerComparator());
            for (String line:kwicList){
                output.writerLine(line);
//            System.out.println(line);
            }
        input.closeReader();
        output.closeWriter();
    }
    private static class AlphabetizerComparator implements Comparator<String> {
        @Override
        public int compare(String o1, String o2) {
            if (o1 == null && o2 == null) {
                throw new NullPointerException();
            }
            int compareValue = 0;
            char o1c = 0; //忽略大小写
            if (o1 != null) {
                o1c = o1.toLowerCase().charAt(0);
            }
            char o2c = o2.toLowerCase().charAt(0); //忽略大小写
            compareValue = o1c - o2c;
            return compareValue;

        }

    }
}

